package com.daoliuhe.scaffold.service.impl;

import com.daoliuhe.scaffold.mapper.UserGroupMapper;
import com.daoliuhe.scaffold.model.UserGroup;
import com.daoliuhe.scaffold.model.UserGroupUser;
import com.daoliuhe.scaffold.service.UserGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserGroupServiceImpl implements UserGroupService {

	private static final Logger logger = LoggerFactory.getLogger(UserGroupServiceImpl.class);
	@Autowired
	UserGroupMapper userGroupMapper;

	@Override
	public Object isRightName(String name, String id) {
		logger.info("isRightName,name:{},id:{}",name,id);
		boolean flag = false;
		if (StringUtils.isEmpty(id)) {
			flag = userGroupMapper.selectByName(name) == null;
		} else {
			UserGroup userGroup = userGroupMapper.selectByPrimaryKey(new BigDecimal(id));
			if (userGroup != null) {
				flag = userGroup.getName().equals(name);
				if (!flag) {
					flag = userGroupMapper.selectByName(name) == null;
				}
			}
		}
		return flag;
	}

	@Override
	public void saveOrUpdate(UserGroup userGroup) {
		logger.info("saveOrUpdate,userGroup:{}",userGroup);
		if (!StringUtils.isEmpty(userGroup.getId())) {
			userGroupMapper.updateByPrimaryKey(userGroup);
		} else {
			userGroupMapper.insert(userGroup);
		}
	}

	@Override
	public Map<String, Object> getPageData(UserGroup userGroup) {
		logger.info("getPageData,userGroup:{}",userGroup);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = userGroupMapper.getPageCount(userGroup);
		map.put("total", total);
		int curPage = userGroup.getPage();
		int rows = userGroup.getRows();
		int maxPage = (total + rows - 1) / rows;
		if (curPage > maxPage && maxPage > 0) {
			userGroup.setPage(maxPage);
		}
		map.put("rows", userGroupMapper.getPageData(userGroup));

		return map;
	}

	@Override
	public UserGroup getUserGroupById(BigDecimal id) {
		return userGroupMapper.selectByPrimaryKey(id);
	}


	@Override
	public void updateBatchEnable(ArrayList<String> ids) {
		logger.info("batchEnable,ids:{}",ids);
		userGroupMapper.updateBatchEnable(ids);
	}

	@Override
	public void updateBatchDisable(ArrayList<String> ids) {
		logger.info("batchDisable,ids:{}",ids);
		userGroupMapper.updateBatchDisable(ids);
	}

	@Override
	public Object getUserGroupPageData(UserGroupUser ugu) {
		logger.info("getUserGroupPageData,UserGroupUser:{}",ugu);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = userGroupMapper.getUserGroupPageCountByUser(ugu);
		map.put("total", total);
		int curPage = ugu.getPage();
		int rows = ugu.getRows();
		int maxPage = (total + rows - 1) / rows;
		if (curPage > maxPage && maxPage > 0) {
			ugu.setPage(maxPage);
		}
		map.put("rows", userGroupMapper.getUserGroupPageDataByUser(ugu));

		return map;
	}
}
